#include <QtGui>
#include <QtSql>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "kongm.h"
#include "kaddgame.h"
#include "ksysprofiles.h"
#include "kfirstrun.h"
#include "xfire/kxfire.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    updateGameList();

    createActions();
    createTrayIcon();

    kXFire *xfire = new kXFire();

    gameNames = xfire->getGameNames();

    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
    this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

    connect(ui->checkBoxXFireDebug, SIGNAL(toggled(bool)), xfire, SLOT(showDebugWindow(bool)));

    trayIcon->show();

    trayIcon->showMessage( tr("KON Game Manager"), tr("KON is running."), QSystemTrayIcon::Information, 2000 );

    QSettings settings( "GentooXativa" , "KON Game Manager" );
    ui->checkBoxXFireDebug->setChecked(settings.value("XFireDebug").toBool());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnAddGame_clicked()
{
    kAddGame *addgame = new kAddGame(this);
    addgame->setGameNamesSTL( gameNames );
    addgame->exec();
    updateGameList();
}

void MainWindow::updateGameList()
{
    // We use this variable to check the column name for view,
    // when we add a column we can add easy without assign column
    // count each time :)
    int x = 0;

    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("SELECT name, path, savepath, lupdate,upload,autosync FROM games");
    model->setHeaderData(x, Qt::Horizontal, tr("Name"));
    model->setHeaderData(++x, Qt::Horizontal, tr("Path"));
    model->setHeaderData(++x, Qt::Horizontal, tr("Savegame Path"));
    model->setHeaderData(++x, Qt::Horizontal, tr("Last update"));
    model->setHeaderData(++x, Qt::Horizontal, tr("Upload"));
    model->setHeaderData(++x, Qt::Horizontal, tr("Autosync"));
    ui->tableView->setModel(model);
}

void MainWindow::on_btnDeleteGame_clicked()
{
    bool hasSelection = ui->tableView->selectionModel()->hasSelection();

    if( !hasSelection )
    {
        QMessageBox::critical( this, "Error", "You need to select a game." );
        return;
    }

     QModelIndexList indexes = ui->tableView->selectionModel()->selectedIndexes();
     QModelIndex index;
     QString gname, gpath;

     foreach(index, indexes) {
         if( index.column() == 0 )
         {
            gname = index.data().toString();
         }

         if( index.column() == 1 )
         {
            gpath = index.data().toString();
         }


        }
            QSqlQuery query;
            query.prepare( "DELETE FROM games WHERE name = :name AND path = :path" );
            query.bindValue( ":name", gname );
            query.bindValue( ":path", gpath );

            int ret = QMessageBox::question( this, "Are you sure?", "You want to delete this game from list?", QMessageBox::Yes | QMessageBox::No );

            switch( ret )
            {
                case QMessageBox::Yes:
                    query.exec();
                    QMessageBox::information( this, "Game deleted from list", QString( "Game %1 deleted from list." ).arg( gname ));
                break;

                case QMessageBox::No:
                    return;
                break;

                default:
                break;
            }

     updateGameList();
}

void MainWindow::on_actionAdd_games_triggered()
{
    kAddGame *addgame = new kAddGame(this);

    addgame->show();
/*
    if( !addgame->gAdded().isEmpty() )
        trayIcon->showMessage( tr("KON Game Manager"), QString("Game <b>%1</b> added!").arg( addgame->gAdded()), QSystemTrayIcon::Information, 2000 );
    else
        trayIcon->showMessage( tr("KON Game Manager"), tr("Error adding game!"), QSystemTrayIcon::Information, 2000 );
*/
    updateGameList();
}

void MainWindow::on_actionSystem_profiles_triggered()
{
    kSysProfiles *prof = new kSysProfiles(this);
    prof->exec();

}

void MainWindow::createTrayIcon()
{
     trayIconMenu = new QMenu(this);
     trayIconMenu->addAction(minimizeAction);
     trayIconMenu->addAction(maximizeAction);
     trayIconMenu->addAction(restoreAction);
     trayIconMenu->addSeparator();
     trayIconMenu->addAction(quitAction);

     trayIcon = new QSystemTrayIcon(this);
     trayIcon->setContextMenu(trayIconMenu);

     trayIcon->setIcon(QIcon(":/images/kon.png"));
}

void MainWindow::createActions()
{
     minimizeAction = new QAction(tr("Mi&nimize"), this);
     connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

     maximizeAction = new QAction(tr("Ma&ximize"), this);
     connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

     restoreAction = new QAction(tr("&Restore"), this);
     connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

     quitAction = new QAction(tr("&Quit"), this);
     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
     switch (reason) {
     case QSystemTrayIcon::Trigger:
          //trayIcon->showMessage( tr("KON Game Manager"), tr("Trigger????"), QSystemTrayIcon::Information, 2000 );
     case QSystemTrayIcon::DoubleClick:
         showNormal();
         break;
     case QSystemTrayIcon::MiddleClick:
         qApp->quit();
         break;
     default:
         ;
     }
}

void MainWindow::on_actionPreferences_triggered()
{

}

void MainWindow::on_pushButton_4_clicked()
{
    kFirstRun *f = new kFirstRun(this);
    f->show();
    _DEBUG_
}
